package com.ghy.controller;

import com.ghy.constant.ML;
import com.ghy.dto.EpisodeInsertDTO;
import com.ghy.dto.EpisodePageDTO;
import com.ghy.dto.EpisodeUpdateDTO;
import com.ghy.entity.Episode;
import com.ghy.excel.EpisodeExcelDTO;
import com.ghy.result.Result;
import com.ghy.util.EasyExcelUtil;
import com.ghy.vo.EpisodeSimpleListVO;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import com.ghy.entity.Episode;
import com.ghy.service.EpisodeService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 集次表 控制层。
 *
 * @author ghy
 * @since 2024-12-22
 */
@Tag(name = "集次")
@RestController
@RequestMapping("/episode")
public class EpisodeController {

    @Autowired
    private EpisodeService episodeService;

    @Operation(summary ="新增——单条",description ="新增一条集次记录")
    @PostMapping("insert")
    public boolean insert(@Validated @RequestBody EpisodeInsertDTO dto) {
        return episodeService.insert(dto);
    }

    @Operation(summary ="查询——单条",description ="按主键查询一条集次记录")
    @GetMapping("select/{id}")
    public Episode select(@PathVariable("id") Long id) {
        return episodeService.select(id);
    }

    @Operation(summary = "查询——简单查询全部", description = "查询全部集次记录")
    @GetMapping("simpleList")
    List<EpisodeSimpleListVO> simpleList(){
        return episodeService.simpleList();
    }

    @Operation(summary ="查询——分页",description ="分页查询集次记录")
    @GetMapping("page")
    public Page<Episode> page(@Validated EpisodePageDTO dto) {
        return episodeService.page(dto);
    }

    @Operation(summary="修改——单条",description="按主键修改一条集次记录")
    @PutMapping("update")
    public boolean update(@RequestBody EpisodeUpdateDTO dto) {
        return episodeService.update(dto);
    }

    @Operation(summary ="删除——单条",description ="按主键删除一条集次记录")
    @DeleteMapping("delete/{id}")
    public boolean delete(@PathVariable("id") Long id) {
        return episodeService.delete(id);
    }

    @Operation(summary="删除——批量",description ="按主键批量删除集次记录")
    @DeleteMapping("deleteBatch")
    public boolean deleteBatch(@RequestParam("ids") List<Long> ids) {
        return episodeService.deleteBatch(ids);
    }

    @Operation(summary = "修改——集次封面图片", description = "选图片")
    @PostMapping("uploadVideoCover/{id}")
    public Result<String> uploadVideoCover(@RequestParam("videoCoverFile") MultipartFile videoCoverFile, @PathVariable("id") Long id){

        return new Result<>(episodeService.uploadVideoCover(videoCoverFile,id));
    }

    @Operation(summary = "修改——集次封面视频", description = "选图片")
    @PostMapping("uploadVideo/{id}")
    public Result<String> uploadVideo(@RequestParam("videoFile") MultipartFile videoFile, @PathVariable("id") Long id){

        return new Result<>(episodeService.uploadVideo(videoFile,id));
    }

    @Operation(summary="查询——报表打印",description="打印集次相关的报表数据")
    @GetMapping("excel")
    public void excel(HttpServletResponse response){
        EasyExcelUtil.download(response,
                ML.Episode.EXCEL_FILE_NAME,
                ML.Episode.EXCEL_SHEET_NAME,
                EpisodeExcelDTO.class,
                episodeService.excel());
    }
    
}
